// 2025/10/11: 首个版本

using System;
using System.Linq;
using System.Collections.Generic;
using ASEva;
using ASEva.UIAvalonia;
using ASEva.Utility;

namespace CommonSource
{
    partial class MultiSelect : DialogPanel
    {
        public MultiSelect() // For designer
        {
            InitializeComponent();
        }

        public MultiSelect(string message, string[] options, int[] defaultSelectIndices, String iconResourceName = null)
        {
            InitializeComponent();
            SetFixMode(400, Math.Min(400, 100 + 40 * options.Length), true);
            Icon = ResourceLoader.Load(iconResourceName)?.ToAvaloniaWindowIcon();
            Title = message;
            SelectedIndices = defaultSelectIndices;

            var selectedList = defaultSelectIndices.ToList();

            var items = new CheckItem[options.Length];
            for (int i = 0; i < options.Length; i++)
            {
                items[i] = new CheckItem
                {
                    Content = options[i],
                    IsChecked = selectedList.Contains(i),
                };
            }
            checkListBox.ItemsSource = items;

            button.Click += delegate
            {
                var selected = new List<int>();
                for (int i = 0; i < items.Length; i++)
                {
                    if (items[i].IsChecked) selected.Add(i);
                }
                SelectedIndices = selected.ToArray();
                Close();
            };
        }

        public int[] SelectedIndices { get; private set; }

        private class CheckItem
        {
            public String Content { get; set; }

            public bool IsChecked { get; set; }
        }
    }
}